<template>
  <div class="app-container">
    <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
    <div style="margin: 15px 0;"/>
    <el-checkbox-group v-model="checkedRoles" @change="handlecheckedRolesChange">
      <el-checkbox v-for="role in roles" :label="role.id" :key="role.id">{{ role.name }}</el-checkbox>
    </el-checkbox-group>
    <br>
    <el-button :disabled="saveBtnDisabled" type="primary" @click="update">保存</el-button>

  </div>
</template>

<script>

import userApi from '@/api/acl/user'

export default {

  data() {
    return {
      checkAll: false,
      checkedRoles: [],
      roles: [],
      isIndeterminate: true,
      userId: '',
      saveBtnDisabled: false
    }
  },

  created() {
    this.init()
  },

  methods: {

    init() {
      if (this.$route.params && this.$route.params.id) {
        this.userId = this.$route.params.id
        this.getById(this.userId)
      }
    },

    getById(userId) {
      userApi.getAssign(userId).then(response => {
        var jsonObj = response.result.assignRoles
        this.checkedRoles = this.getJsonToList(jsonObj, 'id')
        this.roles = response.result.allRolesList
      })
    },

    getJsonToList(json, key) {
      var list = JSON.parse(JSON.stringify(json))
      var strText = []
      for (var i = 0; i < list.length; i++) {
        strText.push(list[i][key])
      }
      return strText
    },

    handleCheckAllChange(val) {
      this.checkedRoles = val ? this.roles.map(role => role.id) : []
      this.isIndeterminate = false
    },

    handlecheckedRolesChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.roles.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.roles.length
    },

    update() {
      this.saveBtnDisabled = true
      var ids = this.checkedRoles.join(',')
      userApi.saveAssign(this.userId, ids).then(response => {
        if (response.success) {
          this.$message({
            type: 'success',
            message: '保存成功'
          })
          this.$router.push({ path: '/acl/user/list' })
        }
      })
    }
  }
}
</script>
